package com.zeus.common.utils;

import com.zeus.common.log.ZeusLoggerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * HTTP工具
 *
 * @author wangfeixiong126@gmail.com  2021/8/5 18:59
 */
public class HttpUtils {
    private final static Logger logger = LoggerFactory.getLogger(HttpUtils.class);
    /**
     * unknown
     */
    private static final String UN_KNOWN = "unknown";
    /**
     * ip最大长度
     */
    private static final int IP_MAX_LENGTH = 15;
    /**
     * 逗号
     */
    private static final String COMMA = ",";

    /**
     * 获取HttpServletRequest
     *
     * @return HttpServletRequest
     * @author wangfeixiong126@gmail.com  2021/6/3 17:36
     */
    public static HttpServletRequest getHttpServletRequest() {
        HttpServletRequest request = null;
        try {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if (requestAttributes == null) {
                return null;
            }
            ServletRequestAttributes attributes = (ServletRequestAttributes)requestAttributes;
            // 不然获取不到
            RequestContextHolder.setRequestAttributes(attributes, true);
            request = attributes.getRequest();
        } catch (Throwable ignored) {
        }
        return request;
    }

    /**
     * 根据request获取真实IP地址
     *
     * @param request request
     * @return IP地址
     * @author wangfeixiong126@gmail.com  2021/5/19 17:19
     */
    public static String getIpAddress(HttpServletRequest request) {
        try {
            if (request == null) {
                return UN_KNOWN;
            }
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Forwarded-For");
            }
            if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Real-IP");
            }

            if (ip == null || ip.length() == 0 || UN_KNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ip != null && ip.length() > IP_MAX_LENGTH) {
                if (ip.indexOf(COMMA) > 0) {
                    ip = ip.substring(0, ip.indexOf(COMMA));
                }
            }
            return ip;
        } catch (Exception e) {
            if (logger.isWarnEnabled() && ZeusLoggerConfig.needEnableLogger()) {
                logger.warn("### 根据HttpServletRequest 获取请求地址失败 >> errMsg = {}", e.getMessage());
            }
        }
        return UN_KNOWN;
    }
}
